<?php

/*
 Extended Backus-Naur Form (EBNF)
 symbol ::= expression
 Char   ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
            (: any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. :)
 S      ::= (#x20 | #x9 | #xD | #xA)+
 symbol ::= expression
 symbol ::= expression
 symbol ::= expression */

$name_regexp = "[a-zA-Z_][a-zA-Z0-9_-]*";

if (!empty($_POST["grammar"])) {
    $grammar =& $_POST["grammar"];
    $apos_begin = false;
    $quote_begin = false;
    $comment_begin = false;
    $comment_begin_1 = false;
    $comment_end_1 = false;
    $parsed = "<ebnf>";
    for ( $i=0; $i<strlen($grammar); $i++ ) {
        $char = $grammar[$i];
        if ($apos_begin && $char=="'") {
            $parsed .= "</string>";
            $apos_begin = false;
            continue;
        } elseif ($quote_begin && $char=="\"") {
            $parsed .= "</string>";
            $quote_begin = false;
            continue;
        } elseif ($comment_begin_1) {
            if ($char=="*") {
                $parsed .= "<comment>";
                $comment_begin_1 = false;
                $quote_begin = true;
            }
            continue;
        } elseif ($comment_end_1) {
            if ($char=="/") {
                $parsed .= "</comment>";
                $comment_end_1 = false;
            }
            continue;
        } elseif ($char=="'") {
            $parsed .= "<string>";
            $apos_begin = true;
            continue;
        } elseif ($char=="\"") {
            $parsed .= "<string>";
            $quote_begin = true;
            continue;
        } elseif ($char=="/") {
            $parsed .= "<string>";
            $comment_begin_1 = true;
            continue;
        } else {
            $parsed .= htmlspecialchars($char, ENT_QUOTES, "cp1251");
        }
    }
    $parsed .= "</ebnf>";
    $grammar = pcre::replace("\n", "<!--br-->", $grammar);
    $grammar = pcre::replace("[\s]", " ", $grammar);
    $grammar = pcre::replace(pcre::escape("<!--br-->"), "\n", $grammar);
    echo "<code>".nl2br($grammar)."</code>";
    //debug_out($grammar);
    $withNumeration = true;
    if (count($grammar)>1) {
        $tmp = trim($grammar[0]);
        /*if () {

        }
        $rules[] =
        for ( $i=1; $i<count($grammar); $i++ ) {
        	$rules =
        }*/
    }
}
/*
[18]   	UnionExpr	   ::=   	PathExpr
			| UnionExpr '|' PathExpr
[19]   	PathExpr	   ::=   	LocationPath
			| FilterExpr
			| FilterExpr '/' RelativeLocationPath
			| FilterExpr '//' RelativeLocationPath
[20]   	FilterExpr	   ::=   	PrimaryExpr
			| FilterExpr Predicate

*/

?>